package org.enwey.icarenew.Fragment.New;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import org.enwey.icarenew.API.APIManager;
import org.enwey.icarenew.API.RequestListResult;
import org.enwey.icarenew.APPUtils.ToastUtil;
import org.enwey.icarenew.Activity.MainActivity;
import org.enwey.icarenew.Activity.News.NewDetailActivity;
import org.enwey.icarenew.Adapter.NewAdapter;
import org.enwey.icarenew.Fragment.BaseFragment.BaseFragment;
import org.enwey.icarenew.Model.New;
import org.enwey.icarenew.R;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;

/**
 * Created by enwey on 2014/11/20.
 */
public class NewFragment extends BaseFragment {
    private final String TAG = "NewFragment";
    private NewAdapter mAdapter;
    private List<New> mNewList;
    private PullToRefreshListView mListView;
    private boolean isRefresh;
    private int currentPage = 0;
    private String mOldId;
    private MainActivity mActivity;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        View view = inflater.inflate(R.layout.fragment_new, container, false);
        setUp();
        findView(view);
        registerListener();
        CallAPIGetNewList();
        return view;
    }


    private void findView(View view) {
        mListView = (PullToRefreshListView) view.findViewById(R.id.fragment_new_list);
        mListView.setAdapter(mAdapter);
    }

    @Override
    public void setContentView(LayoutInflater inflater, ViewGroup mLayerContextView) {
        inflater.inflate(R.layout.fragment_new, mLayerContextView);
    }

    private void setUp() {
        mActivity = (MainActivity) getActivity();
        isRefresh = false;
        mOldId = getStyleId();
        mAdapter = new NewAdapter(getActivity());
        mNewList = new ArrayList<New>();
        mAdapter.setNewList(mNewList);
    }

    private void CallAPIGetNewList() {
        if (!isRefresh || getStyleId() != mOldId) {
            mActivity.showLoading();
        }
        Hashtable<String, String> parameters = new Hashtable<>();
        parameters.put("style_id", getStyleId() + "");
        parameters.put("page", currentPage + "");

        APIManager.getInstance(getActivity()).getNewList(parameters, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                if (!isRefresh || getStyleId() != mOldId) {
                    isRefresh = false;
                    mActivity.hideLoading();
                }
                ToastUtil.show(getActivity(), "连接失败");
            }
        }, new Response.Listener<RequestListResult<New>>() {
            @Override
            public void onResponse(RequestListResult<New> response) {
                if (!isRefresh || getStyleId() != mOldId) {
                    isRefresh = false;
                    mActivity.hideLoading();
                }
                mOldId = getStyleId();
                if (response.data != null && response.data.size() > 0) {
                    if (currentPage == 0) {
                        if (isRefresh) {
                            mNewList = response.data;
                        } else {
                            mNewList.addAll(response.data);
                        }
                        mAdapter.setNewList(mNewList);
                    } else if (currentPage > 0 && mNewList != null) {
                        mNewList.addAll(response.data);
                    }
                    mAdapter.notifyDataSetChanged();
                    mListView.onRefreshComplete();
                }

                if (mAdapter.getCount() == 0) {
                } else if (response.code == 201 || response.data.size() < 10) {
                    mListView.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
                } else {
                    mListView.setMode(PullToRefreshBase.Mode.BOTH);
                }
                mListView.onRefreshComplete();
            }
        });

    }

    public void registerListener() {

        mListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                currentPage = 0;
                isRefresh = true;
                CallAPIGetNewList();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                if (mNewList != null) {
                    isRefresh = false;
                    currentPage++;
                    CallAPIGetNewList();
                }
            }
        });

        AdapterView.OnItemClickListener register = new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(getActivity(), NewDetailActivity.class);
                intent.putExtra("style_id", getStyleId());
                intent.putExtra("new_id", mNewList.get(position - 1).id + "");
                startActivity(intent);
            }
        };
        mListView.setOnItemClickListener(register);
    }

    @Override
    public void showFragment() {
        Log.v(TAG,getStyleId()+"--&&--"+mOldId);
        isRefresh = false;
        if (getStyleId() != mOldId) {
            mNewList.clear();
        }
        CallAPIGetNewList();
    }

}
